﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace IntrepidGame.CollisionObjects
{
    public class CollisionObject
    {
        #region Variables Region

        Vector2 position;   //where the object is, from top left corner
        Vector2 origin;     //Where the object is, from middle of object
        Vector2 localOrigin;//Where the center of the object is, local coordinates

        Vector2 size;       //vector2 representation of the Width and Height
        Rectangle bounds;   //The bounding box aroud the object

        string imageName;
        Texture2D imageTexture;
        Color color;

        Rectangle destinationRectangle; //Where on the screen is the current image
        Rectangle sourceRectangle;      //Where in the texture is the current image

        Boolean enabled; //Can it interact with objects
        Boolean visible; //Can it be seen on the screen

        protected static Game1 GameRef;

        #endregion

        #region Properties Region

        public Vector2 Position
        {
            get { return position; }
            set 
            { 
                position = value;
                origin = new Vector2(position.X + size.X / 2, position.Y + size.Y / 2);
                destinationRectangle = new Rectangle((int)position.X, (int)position.Y, destinationRectangle.Width, destinationRectangle.Height);
            }
        }

        public Vector2 Origin
        {
            get { return origin; }
            set 
            { 
                origin = value;
                position = new Vector2(origin.X - size.X / 2, origin.Y - size.Y / 2);
                destinationRectangle = new Rectangle((int)position.X, (int)position.Y, destinationRectangle.Width, destinationRectangle.Height);
            }
        }

        public Vector2 LocalOrigin
        {
            get { return localOrigin; }
            set 
            { 
                localOrigin = value;
                origin = new Vector2((int)(position.X + (localOrigin.X - origin.X)), (int)(position.Y + (localOrigin.Y - origin.X)));
                position = new Vector2(origin.X - size.X / 2, origin.Y - size.Y / 2);
            }
        }

        public Vector2 Size
        {
            get { return size; }
            set
            {
                size = value;
                destinationRectangle.Width = (int)value.X;
                destinationRectangle.Height = (int)value.Y;
            }
        }


        public string ImageName
        {
            get { return imageName; }
        }


        public Texture2D ImageTexture
        {
            get { return imageTexture; }
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public Rectangle DestinationRectangle
        {
            get { return destinationRectangle; }
            set
            {
                destinationRectangle = value;
                position = new Vector2(destinationRectangle.X, destinationRectangle.Y);
                size = new Vector2(destinationRectangle.Width, destinationRectangle.Height);
                origin = new Vector2(position.X + size.X / 2, position.Y + size.Y / 2);
            }
        }

        public Rectangle SourceRectangle
        {
            get { return sourceRectangle; }
            set { sourceRectangle = value; }
        }

        public Boolean Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }

        public Boolean Visible
        {
            get { return visible; }
            set { visible = value; }
        }

        #endregion

        public CollisionObject()
        {
            Enabled = false;
            Visible = false;
        }

        #region Virtual Functions

        public virtual Boolean CollisionWithObject(CollisionObject obj)
        {
            if (Enabled && obj.Enabled)
            {
                if (DestinationRectangle.Intersects(obj.DestinationRectangle))
                {
                    return true;
                }
            }

            return false;
        }

        public virtual void LoadContent(Game1 gameRef, string imagePath, Rectangle source, Rectangle dest, Color tint)
        {
            GameRef = gameRef;

            if (imagePath != null)
                imageName = imagePath;

            imageTexture = GameRef.Content.Load<Texture2D>(ImageName);

            if (source != null)
                SourceRectangle = source;

            if (dest != null)
                DestinationRectangle = dest;

            if (color != null)
                color = tint;
            else
                color = Color.White;
        }

        public virtual void Update(GameTime gameTime)
        {
            //Does nothing
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (Visible)
                spriteBatch.Draw(ImageTexture, DestinationRectangle, SourceRectangle, Color);
        }

        #endregion
    }
}
